import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
// tslint:disable-next-line: ordered-imports
import refresh from '@dfeidao/atom-web/url/refresh';
// tslint:disable-next-line: ordered-imports
import { productid } from '../../atom/config';
import error from '@dfeidao/atom-web/msgbox/error';
import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';
import hide from '@dfeidao/atom-web/ui/hide';

export default async function a005(fd: IFeidaoAiBrowserComponent) {
	// todo//题库类型
	// 获取的题库编号
	const question_no = get(fd, 'arr');


	// 题目类型
	const leixing1 = fd.data.node.querySelector<HTMLSelectElement>('[data-id="leixing"]');
	const topic_type = leixing1.value;
	const re = await nodejs<{dt: Array<{topic: string,right_answer:string,option_a:string,option_b:string,option_c:string,option_d:string}>}>('question-bank/zj-000005/s003',{
		filter:{
	
			topic_type
		}
	});
	

	// 题目内容
	const topic1 = fd.data.node.querySelector<HTMLSelectElement>('[data-id="topic"]');
	const topic = topic1.value;

	// 选项Adata-id="option_a"
	const option_a1 = fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_a"]');
	const option_a = option_a1.value;
	// 选项B
	const option_b1 = fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_b"]');
	const option_b = option_b1.value;
	// 选项C
	const option_c1 = fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_c"]');
	const option_c = option_c1.value;
	// 选项D
	const option_d1 = fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_d"]');
	const option_d = option_d1.value;
	// 选项
	const xuanxiangB = fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangB"]:checked');
	const xuanxiangC = fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangC"]:checked');
	const xuanxiangD = fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangD"]:checked');
	const xuanxiangA = fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangA"]:checked');
	let right_answer = "";
	if (topic_type === "多选题") {
		if (xuanxiangA.length > 0 && xuanxiangC.length > 0 && xuanxiangB.length === 0 && xuanxiangD.length === 0) {

			right_answer = "AC";


		} else if (xuanxiangA.length > 0 && xuanxiangB.length > 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {

			right_answer = "AB";


		} else if (xuanxiangA.length > 0 && xuanxiangB.length > 0 && xuanxiangC.length > 0 && xuanxiangD.length === 0) {

			right_answer = "ABC";



		} else if (xuanxiangA.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length === 0 && xuanxiangB.length === 0) {

			right_answer = "AD";



		} else if (xuanxiangB.length > 0 && xuanxiangC.length > 0 && xuanxiangA.length === 0 && xuanxiangD.length === 0) {

			right_answer = "BC";


		} else if (xuanxiangB.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length === 0 && xuanxiangA.length === 0) {

			right_answer = "BD";


		} else if (xuanxiangB.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length > 0 && xuanxiangA.length === 0) {

			right_answer = "BCD";


		} else if (xuanxiangA.length > 0 && xuanxiangB.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length === 0) {

			right_answer = "ABD";


		} else if (xuanxiangA.length > 0 && xuanxiangC.length > 0 && xuanxiangD.length > 0 && xuanxiangB.length === 0) {

			right_answer = "ACD";

		} else if (xuanxiangA.length > 0 && xuanxiangC.length > 0 && xuanxiangD.length > 0 && xuanxiangB.length > 0) {
			right_answer = "ABCD";
		} else if (xuanxiangA.length > 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
			error("最低选择两项");
			return;


		} else if (xuanxiangA.length === 0 && xuanxiangB.length > 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
			error("最低选择两项");
			return;
		} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length > 0 && xuanxiangD.length === 0) {
			error("最低选择两项");
			return;
		} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length > 0) {
			error("最低选择两项");
			return;


		} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
			error("最低选择两项");

			return;

		} else {

			right_answer = "ABCD";
			return;

		}
	} else {
		if (xuanxiangA.length > 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
			right_answer = "A";



		} else if (xuanxiangA.length === 0 && xuanxiangB.length > 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
			right_answer = "B";




		} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length > 0 && xuanxiangD.length === 0) {
			right_answer = "C";




		} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length > 0) {
			right_answer = "D";


		} else {
			error("请选择一个选项");
			return;
		}

	}
	for (let i=0;i < re.dt.length;i++) {
		if (topic === re.dt[i].topic && right_answer === re.dt[i].right_answer&&option_a===re.dt[i].option_a&&option_b===re.dt[i].option_b&&option_c===re.dt[i].option_c&&option_d===re.dt[i].option_d) {
			return;
		}
		}
	const res = await nodejs<{ code: 1 | 0; result: string }>('question-bank/zj-000005/s002', {
		filter: {
			productid,
			topic_type,
			topic,
			option_a,
			option_b,
			option_c,
			option_d,
			right_answer,
			question_no

		}

	});
// 	const na001=get('na001');
//  const arr=get('arr');
	if (res.code === 1) {
		alert("修改成功");
		refresh();
		// set_node_cls(del,'display-n',true);
		// hide(fd.data.node);
		// if (na001 === 1) {
		// 	fire('zj-000001','a012');
		// } else {
		//    fire('zj-000001','a003');

		// }

	} else {
		alert("修改失败");
	}
}
